import  java.io.*;
import  java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CoffeeShopApplication{

	private static BufferedReader  stdIn =
		new  BufferedReader(new  InputStreamReader(System.in));
	private static PrintWriter  stdOut =
		new  PrintWriter(System.out, true);
	private static PrintWriter  stdErr =
		new  PrintWriter(System.err, true);

	private static String NUMBER = "^[0-9]*$";
	private static String NUMBERDOUBLE = "[0-9]*\\.?[0-9]+";
	private CoffeeShop coffeeShop;

	private static final int MIN_NUMBER = 0;
	
	private static final int MAX_NUMBER = 50;

	private static final double MIN_COST = 2.0;
	
	private static final double MAX_COST = 50.0;

	public static void main(String[]  args) throws IOException  {

		CoffeeShopApplication application = new  CoffeeShopApplication();

		application.run();
	}

	private void run() throws IOException  {

		coffeeShop = new  CoffeeShop();

		int  choice = getChoice();

		while (choice != 0)  {

			if (choice == 1)  {

				coffeeShop.addCoffee(readCoffee());
			} else if (choice == 2)  {

				stdOut.println(coffeeShop.toString());
			} else if (choice == 3)  {

				stdOut.println("Total Cost: " + coffeeShop.getTotalCost());
			}

			choice = getChoice();
		}
	}

	private int  getChoice() throws IOException  {

		/* PLACE YOUR CODE HERE */
		Scanner scanner = new Scanner(System.in);
		int choice = -1;

		do {
			displayMeal();
			choice = scanner.nextInt();

			try {
				if (choice < 0 || choice > 3) {
					throw new IOException("选择不合法");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}while (choice < 0 || choice > 3);
		return choice; //you need to change it.
	}

	private Coffee readCoffee() throws CoffeeFormatException, IdFormatException, NameFormatException, TasteFormatException, CostFormatException, IOException {

		/* PLACE YOUR CODE HERE */
		int id = -1;
		String name = null;
		String taste = null;
		double cost = 0;
		while(true) {
			try {
				System.out.print("请输入您要添加的咖啡(XX_XX_XX_XX) :> ");
				String s = stdIn.readLine();
				String[] strings = s.split("_");

				//输入下划线分割的单词多于或少于4个均显示错误信息
				if (strings.length != 4) {
					throw new CoffeeFormatException("输入的商品的属性个数不合规范");
				}

				//检查输入的id是否合法
				if (checkId(strings[0])) {
					id = Integer.parseInt(strings[0]);
				} else {
					throw new IdFormatException("输入的id格式或大小不合规范");
				}

				//检查输入的name是否合法
				if(checkName(strings[1])){
					name = strings[1];
				}else{
					throw new NameFormatException("输入的name不能为null");
				}

				//检查输入的taste是否合法
				if(checkTaste(strings[2])){
					taste = strings[2];
				}else{
					throw new TasteFormatException("输入的taste不能为null");
				}

				//检查输入的cost是否合法
				if(checkCost(strings[3])){
					cost = Double.parseDouble(strings[3]);
				}else{
					throw new CostFormatException("输入的cost格式或者大小不合规范");
				}
				//如输入的信息有效，则退出循环
				break;
			} catch (IOException e) {
				e.printStackTrace();
			} catch (CoffeeFormatException e) {
				e.printStackTrace();
			}catch (IdFormatException e){
				e.printStackTrace();
			}catch (NameFormatException e){
				e.printStackTrace();
			}catch (TasteFormatException e){
				e.printStackTrace();
			}catch (CostFormatException e){
				e.printStackTrace();
			}
			System.out.println("请重新输入");
		}
		return new Coffee(id,name,taste,cost); //you need to change it.
	}

	private void displayMeal(){
		System.out.println("0 -> 终止程序");
		System.out.println("1 -> 添加咖啡");
		System.out.println("2 -> 显示咖啡店所有咖啡的信息");
		System.out.println("3 -> 显示商店中所有咖啡的总成本");
		System.out.print("请输入您的选择(0-3) :> ");
	}

	private boolean checkId(String str){
		Pattern pattern = Pattern.compile(NUMBER);
		Matcher matcher = pattern.matcher(str);
		if(matcher.matches()){
			int id = Integer.parseInt(str);
			//id小于0或者大于50都不合法
			if(id >=0 && id <= 50){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}

	private boolean checkCost(String str){
		Pattern pattern = Pattern.compile(NUMBERDOUBLE);
		Matcher matcher = pattern.matcher(str);
		if(matcher.matches()){
			double cost = Double.parseDouble(str);
			//cost小于0或者大于50都不合法
			if(cost >=2.0 && cost <= 50.0){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}

	private boolean checkName(String str){
		if(str == null){
			return false;
		}else{
			return true;
		}
	}

	private boolean checkTaste(String str){
		if(str == null){
			return false;
		}else{
			return true;
		}
	}
}